# -*- coding: utf-8 -*-
# @Time    : 2020/11/16 16:56
# @Author  : YOGA
# @File    : 网易云自动登录.py
# @Software: PyCharm

import time
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

# opt = Options()
# opt.add_argument("--headless")
# opt.add_argument("--disable-gpu")

# driver = webdriver.Chrome(options=opt)
driver = webdriver.Chrome()
driver.get("https://music.163.com/")
#全屏网页
driver.maximize_window()
# time.sleep(3)
'''
selenium的三种等待方式：
1、强制等待
time.sleep(seconds)

2、隐式等待：等待页面全部加载完就执行
driver.implicitly_wait(max_seconds)
参数为最多等待的时间

3、显式等待：显示等待是针对某一个元素进行相关等待判定
element = WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'su')))
一共等待 5 秒钟，每 0.5s 找一次，直到通过 ID 找到

'''
driver.implicitly_wait(10)
#点击页面登录
driver.find_element_by_xpath("//a[text()='登录']").click()
# time.sleep(3)
#选择其他方式登录
driver.find_element_by_xpath("//a[text()='选择其他登录模式']").click()
# time.sleep(3)
#勾选同意协议
driver.find_element_by_xpath("//input[@id='j-official-terms']").click()
# time.sleep(3)
#选择手机登录
driver.find_element_by_xpath("//a[@class='_3fo6oHZe _10mxG2UY _1Gh25bMk']").click()
# time.sleep(3)
#输入用户名和密码
# driver.find_element_by_xpath("//input[@class='_2OT0mQUQ']").send_keys("18255719926")
driver.find_element_by_xpath("//input[@class='_2OT0mQUQ']").send_keys("18830251032")
driver.find_element_by_xpath("//a[@class='_1XRPS4yQ']").click()

verification_code = input("请输入验证码: ")
driver.find_element_by_xpath("//input[@class='sR89MU1J']").send_keys(verification_code)
# time.sleep(3)
#取消选择自动登录
driver.find_element_by_xpath("//input[@type='checkbox']").click()
# time.sleep(3)
#点击登录
driver.find_element_by_xpath("//a[@class='_3fo6oHZe _10mxG2UY _19WWNTbu']").click()

#获取我喜欢的音乐 / 歌单
# driver.find_element_by_xpath("//a[@data-module='my']").click()
#1818333531 1650955904687
#29771420   1650955904687
iframe = driver.find_element_by_xpath('//*[@id="g_iframe"]')
driver.switch_to.frame(iframe)
#点击签到
time.sleep(3)
try:
    driver.find_element_by_xpath("//a[@class='sign u-btn2 u-btn2-2']").click()
except:
    print("已签到")
finally:
    print('当天已成功签到 O(∩_∩)O')
time.sleep(3)
#进入用户主页面
try:

    user_name = driver.find_element_by_xpath("//a[@id='j-vip-code-to-home']").text
    print(user_name)
except:
    print('用户名有问题')
page = driver.page_source
try:
    driver.find_element_by_xpath("//a[@class='head f-pr']").click()
    user_pic_src = driver.find_element_by_xpath('//*[@id="ava"]/img').get_attribute('src')
except:
    print('图片有问题')

with open('./'+user_name+'.jpg', mode='wb') as f:
    f.write(requests.get(user_pic_src).content)
time.sleep(5)
driver.switch_to.default_content()
iframe = driver.find_element_by_xpath('//*[@id="g_iframe"]')
driver.switch_to.frame(iframe)
page1 = driver.page_source
print(page1)
import re
obj_username = re.compile('title="(?P<username>.*?)喜欢的音乐">.*?喜欢的音乐</a></p>')
user_name = obj_username.findall(page1)[0]
print(user_name)
try:
#     driver.find_element_by_xpath(f'//a[text()="{user_name}喜欢的音乐"]').click()
# except:
#     print("00")
# try:
#     # driver.find_element_by_xpath(f"//a[@title={user_name}'喜欢的音乐']").click()
#     driver.find_element_by_xpath('//*[@id="cBox"]/li[0]/div[0]/a').click()
#     #//*[@id="auto-id-8DVzytkwH6HgwF53"]/p/a
# except:
#     print("01")
# try:
#     # driver.find_element_by_xpath(f"//a[@title={user_name}'喜欢的音乐']").click()
#     driver.find_element_by_xpath(f'//*a[text()="{user_name}喜欢的音乐"]').click()
    a = driver.find_element_by_xpath(f"//a[@title='{user_name}喜欢的音乐']")
    print(a.get_attribute('href'))
    b = a.get_attribute('href')
    a.click()
except:
    print("02")
b = f'"{b}"'
js=f'window.open({b});'
driver.execute_script(js)
# driver.switch_to.default_content()
iframe = driver.find_element_by_xpath('//*[@id="g_iframe"]')
driver.switch_to.frame(iframe)
#选择其他方式登录
driver.find_element_by_xpath("//a[text()='选择其他登录模式']/..").click()
# time.sleep(3)
#勾选同意协议
driver.find_element_by_xpath("//input[@id='j-official-terms']").click()
# time.sleep(3)
#选择手机登录
driver.find_element_by_xpath("//a[@class='_3fo6oHZe _10mxG2UY _1Gh25bMk']").click()
# time.sleep(3)
#输入用户名和密码
# driver.find_element_by_xpath("//input[@class='_2OT0mQUQ']").send_keys("18255719926")
driver.find_element_by_xpath("/html/body/div[6]/div/div[2]/div/div[2]/section/div[1]/div/input").send_keys("18255719926")
driver.find_element_by_xpath("//a[@class='_1XRPS4yQ']").click()

verification_code = input("请输入验证码: ")
driver.find_element_by_xpath("//input[@class='sR89MU1J']").send_keys(verification_code)
# time.sleep(3)
#取消选择自动登录
driver.find_element_by_xpath("//input[@type='checkbox']").click()
# time.sleep(3)
#点击登录
driver.find_element_by_xpath("//a[@class='_3fo6oHZe _10mxG2UY _19WWNTbu']").click()
try:

    iframe = driver.find_element_by_xpath('//*[@id="g_iframe"]')
    driver.switch_to.frame(iframe)
    page = driver.page_source
    print(page)
except:
    print("1")
try:
    import re
    #匹配文本
    obj1 = re.compile(r'<ul class="f-hide">(?P<ul>.*?)</li></ul>', re.S)
    obj2 = re.compile(r'">(?P<music_name>.*?)</a>', re.S)
    obj3 = re.compile(r'id=(?P<music_id>.*?)"', re.S)
    m = obj1.findall(page)
    print(m[0])
    music_name = obj2.findall(m[0])
    music_id = obj3.findall(m[0])
    print(music_id)
    print(music_name)
    print(len(music_id))
    print(len(music_name))
except:
    print("2")
#获取听歌排行
#https://p1.music.126.net/3_al1Nt1A4RO8Zm0xCC6dg==/109951165279927894.jpg?param=180y180
#https://p1.music.126.net/fTg-qvnkmk2qCxriwYBUQA==/109951167339095025.jpg?param=180y180
# driver.find_element_by_id('srch').send_keys("心恋")
# driver.find_element_by_id('srch').send_keys(Keys.ENTER)
# # time.sleep(5)
#
# '''
# 注意注意注意注意这个是重点！！！
# 因为搜索之后跳转到了新的页面，所以必须要driver.switch_to.frame！！！
# '''
# driver.switch_to.frame("g_iframe")
# ele = driver.find_element_by_xpath('//*[@id="song_1487511998"]')
# # time.sleep(2)
# # driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div/div/div[1]/div[1]/div/a').click()
# # play = driver.find_element_by_id("song_1487511998")
# print(ele)
# # time.sleep(2)
# ele.click()
#
# # driver.find_element_by_class_name('ply').click()



